<template>
	<view>
		<view class="item">
			<view class="left_text">头像</view>
			<view class="right">
				<image :src="myinfo.wxPhotoURL"></image>
				<image class="right_icon" src="@/static/right@2.png" mode=""></image>
			</view>
		</view>
		<view class="item">
			<view class="left_text">手机号</view>
			<view class="right" v-if="myinfo.mainPhone">
				{{myinfo.mainPhone}}
			</view>
			<view class="right" v-else style="color: red;"  @click="openmobileShow()">
				点击绑定手机号
			</view>
		</view>
		<view class="item" style="margin-bottom: 24rpx;">
			<view class="left_text">昵称</view>
			<view class="right">
				<!--  @click="openNickName" -->
				<text>{{myinfo.name}}</text>
				<image class="right_icon" src="@/static/right@2.png" mode=""></image>
			</view>
		</view>
		<!-- <view class="item">
			<view class="left_text">金福提货券</view>
			<view class="right" @click="navBankCard">
				<text>去绑定</text>
				<image class="right_icon" src="@/static/right@2.png"></image>
			</view>
		</view> -->
		<view class="item">
			<view class="left_text">会员等级</view>
			<view class="right">
				<text>白银会员</text>
				<image class="right_icon" src="@/static/right@2.png" mode=""></image>
			</view>
		</view>
		<view class="item">
			<view class="left_text">性别</view>
			<view class="right">
				<!--  @click="openSex" -->
				<text>{{myinfo.sex==0?'未知':myinfo.sex==1?'男':'女'}}</text>
				<image class="right_icon" src="@/static/right@2.png" mode=""></image>
			</view>
		</view>
		<!-- <view class="item">
			<view class="left_text">生日</view>
			<view class="right" @click="openDate">
					<text v-if="date">{{date}}</text>
					<text class="datetime" v-else>请选择日期</text>
				<image class="right_icon" src="@/static/right@2.png" mode=""></image>
			</view>
		</view> -->


		<!-- 昵称弹出层 -->
		<uni-popup ref="nickname" type="dialog">
			<uni-popup-dialog mode="input" title="昵称" placeholder="限4-16个字符，一个汉字为2字符" @confirm="confirmNickname">
			</uni-popup-dialog>
		</uni-popup>
		<!-- 性别选择 -->
		<uni-popup ref="sex" :safe-area="false">
			<view class="sex_box">
				<view class="title">
					<text></text>
					<text>选择性别</text>
					<image src="@/static/gb.png" @click="closeSex"></image>
				</view>
				<view class="content">
					<picker-view @change="bindChangeSex" class="picker" indicator-class="pickerActive">
						<picker-view-column>
							<view class="picker_item" v-for="(item,index) in sexList" :key="index">{{item}}</view>
						</picker-view-column>
					</picker-view>
				</view>
				<view class="btn" @click="bindSex">确定</view>
			</view>
		</uni-popup>
		<!-- 时间选择 -->
		<uni-popup ref="date" :safe-area="false">
			<view class="sex_box">
				<view class="title">
					<text></text>
					<text>选择生日</text>
					<image src="@/static/gb.png"></image>
				</view>
				<view class="content">
					<picker-view indicator-class="pickerActive" :value="value" @change="bindChangeDate" class="picker">
						<picker-view-column>
							<view class="picker_item" v-for="(item,index) in years" :key="index">{{item}}年</view>
						</picker-view-column>
						<picker-view-column>
							<view class="picker_item" v-for="(item,index) in months" :key="index">{{item}}月</view>
						</picker-view-column>
						<picker-view-column>
							<view class="picker_item" v-for="(item,index) in days" :key="index">{{item}}日</view>
						</picker-view-column>
					</picker-view>
				</view>
				<view class="btn" @click="bindDate">确定</view>
			</view>
		</uni-popup>
		<view class="loginbox" v-if="mobileShow">
			<view class="logininfo">
				<view class="lohintitle">请先绑定手机号</view>
				<view class="loginbut">
					<view class="butone" @click="onNavigateBack()">取消</view>
					<view class="buttwo">
						获取手机号
						<button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber"></button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			const date = new Date()
			const years = []
			const year = date.getFullYear()
			const months = []
			const month = date.getMonth() + 1
			const days = []
			const day = date.getDate()
			for (let i = 1990; i <= date.getFullYear(); i++) {
				years.push(i)
			}
			for (let i = 1; i <= 12; i++) {
				months.push(i)
			}
			for (let i = 1; i <= 31; i++) {
				days.push(i)
			}
			return {
				nickname: "GJZ",
				sex: "男",
				sexCopy: "", //临时存储的性别
				date: "",
				sexList: ["男", "女", "保密"],
				years,
				year,
				months,
				month,
				days,
				day,
				value: [9999, month - 1, day - 1],
				myinfo:{},
				mobileShow: false,
			};
		},
		onShow() {
			this.getmyinfo()
		},
		methods: {
			getmyinfo(){
				this.$http({
					url:'api/Wechat/userinfo'
				}).then(res=>{
					this.myinfo = res.data
				})
			},
			openmobileShow(){
				if(!this.myinfo.mainPhone){
					this.mobileShow = true
				}
			},
			// 修改名字
			openNickName() {
				this.$refs.nickname.open("center")
			},
			// 确认修改名字
			confirmNickname(e) {
				this.nickname = e
			},
			// 金福提货券
			navBankCard() {
				uni.navigateTo({
					url: "/personalData/bankCard/bankCard"
				})
			},
			// 选择性别
			openSex() {
				this.$refs.sex.open("bottom")
			},
			// 关闭性别选择
			closeSex() {
				this.$refs.sex.close()
			},
			// 性別change事件
			bindChangeSex(e) {
				this.sexCopy = this.sexList[e.detail.value]
			},
			// 確定選擇性別
			bindSex() {
				this.sex = this.sexCopy
				this.$refs.sex.close()
			},
			// 打開生日选择弹框
			openDate() {
				this.$refs.date.open("bottom")
			},
			// 選額生日
			bindChangeDate(e) {
				let val = e.detail.value
				this.year = this.years[val[0]] 
				this.month = this.months[val[1]] 
				this.day = this.days[val[2]]
			},
			// 确认生日
			bindDate(){
				this.date  = `${this.year}年${this.month}月${this.day}日`
				this.$refs.date.close()
			},
			//绑定手机号
			getPhoneNumber(e) {
				console.log(e, 'e')
				this.mobileShow = false
				this.$http({
					url: 'api/Wechat/bind_mobile',
					data: {
						code: e.detail.code,
					}
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
					this.getmyinfo()
				})
			},
			onNavigateBack(delta = 1) {
				// uni.switchTab({
				// 	url:'/pages/index/index'
				// })
				const pages = getCurrentPages()
				console.log(pages)
				
				if (pages.length > 1) {
				  uni.navigateBack({
					delta: Number(delta || 1)
				  })
				} else {
					uni.switchTab({
						url: '/pages/index/index'
					})
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.item {
		width: 750rpx;
		height: 108rpx;
		background: #FFFFFF;
		border-radius: 14rpx;
		padding: 0 30rpx;
		margin-bottom: 10rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
		align-items: center;

		.left_text {
			font-size: 28rpx;
			font-family: SourceHanSansCN-Regular-, SourceHanSansCN-Regular;
			font-weight: normal;
			color: #555555;
			line-height: 1em;
		}

		.right {
			display: flex;
			align-items: center;
			gap: 0 20rpx;
			font-size: 28rpx;
			font-family: SourceHanSansCN-Regular-, SourceHanSansCN-Regular;
			font-weight: normal;
			color: #222222;

			image {
				width: 76rpx;
				height: 76rpx;
				border-radius: 110rpx;
				border: 2rpx solid #F3F2F2;
			}

			.datetime {
				font-size: 28rpx !important;
				font-family: SourceHanSansCN-Regular-, SourceHanSansCN-Regular !important;
				font-weight: normal !important;
				color: #888888 !important;
				line-height: 1em !important;
			}

			.right_icon {
				width: 8rpx;
				height: 16rpx;
			}
		}
	}

	::v-deep .uni-dialog-title-text {
		font-size: 32rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #222222;
	}

	::v-deep .uni-border-left {
		background: linear-gradient(90deg, #F94D29 0%, #F99529 100%);

	}

	::v-deep .uni-button-color {
		color: #fff;
	}



	.sex_box {
		width: 750rpx;
		height: 590rpx;
		background: #FFFFFF;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;


		.title {
			width: 100%;
			height: 112rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: 2rpx solid #DBDBDB;
			padding: 0 30rpx;
			box-sizing: border-box;
			flex-shrink: 0;

			text {
				font-size: 32rpx;
				font-family: Source Han Sans CN-Medium, Source Han Sans CN;
				font-weight: 500;
				color: #222222;
				line-height: 1em;
			}

			image {
				width: 40rpx;
				height: 40rpx;
				border-radius: 50%;
			}
		}

		.content {
			height: 100%;

			.picker {
				width: 100%;
				height: 100%;

				.picker_item {
					display: flex;
					justify-content: center;
					align-items: center;
				}
			}
		}

		.btn {
			width: 690rpx;
			height: 84rpx;
			background: linear-gradient(0deg, #F99529 0%, #F94D29 100%);
			border-radius: 83rpx;
			margin: 0 30rpx 24rpx 30rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 32rpx;
			font-family: Source Han Sans CN-Medium, Source Han Sans CN;
			font-weight: 500;
			color: #FFFFFF;
			flex-shrink: 0;
		}
	}

	::v-deep .pickerActive {
		height: 90rpx;
	}
	.loginbox {
		width: 100%;
		height: 100%;
		background-color: rgba(0, 0, 0, 0.5);
		position: fixed;
		z-index: 99;
		top: 0;
	
		// left: 0rpx;
		.logininfo {
			// position: fixed;
			width: 560rpx;
			margin: auto;
			height: 250rpx;
			background-color: #ffffff;
			padding: 60rpx 50rpx 10rpx 50rpx;
			margin-top: 552rpx;
			border-radius: 20rpx;
	
			.lohintitle {
				width: 360rpx;
				font-size: 36rpx;
				font-family: Source Han Sans CN;
				font-weight: 500;
				color: #222222;
				margin: auto;
				margin-bottom: 60rpx;
				text-align: center;
			}
	
			.loginbut {
				display: flex;
				justify-content: space-around;
	
				.butone {
					width: 190rpx;
					padding: 0 30rpx;
					height: 86rpx;
					line-height: 86rpx;
					text-align: center;
					border: 2rpx solid #CB9852;
					border-radius: 43rpx;
					font-size: 34rpx;
					font-family: Source Han Sans CN;
					font-weight: 400;
					color: #CB9852;
				}
	
				.buttwo {
					position: relative;
					padding: 0 30rpx;
					width: 190rpx;
					height: 86rpx;
					line-height: 86rpx;
					text-align: center;
					background: linear-gradient(180deg, #CB9852 0%, #CB9852 100%);
					border-radius: 43rpx;
					font-size: 34rpx;
					font-family: Source Han Sans CN;
					font-weight: 400;
					color: #fff;
	
					>button {
						position: absolute;
						opacity: 0;
						left: 0;
						top: 0;
						width: 100%;
						height: 100%;
					}
				}
			}
		}
	}
</style>
